package com.yixun.qfbao.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import com.yixun.qfbao.apidto.PremisesIntentionResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.hander.*;
import com.yixun.qfbao.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import com.yixun.qfbao.annotations.CheckToken;
import com.yixun.qfbao.annotations.V10Log;
import com.yixun.qfbao.apidto.EstateAdviserDto;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.utils.JSONUtil;


@Controller
@ResponseBody
@RequestMapping("/v10")
public class V10ApiController {
    private static final Logger logger = LoggerFactory.getLogger(V10ApiController.class);
    
    @Autowired
    GetTokenHander getTokenForHander;
    
    @Autowired
    ReceiveIntentionHander receiveIntentionHander;
    
    @Autowired
    ReceiveVisitHander receiveVisitHander;
    
    @Autowired
    ReceiveIntentionFollowAffixHander receiveIntentionFollowAffixHander;
    
    @Autowired
    ReceiveAffixHander receiveAffixHander;
    
    @Autowired
	private ListEstateAdviserByPremisesIdHandler listEstateAdviserByPremisesIdHandler;

    @Autowired
    QueryIntentionStatusHander queryIntentionStatusHander;

    @Autowired
    ReceiveIntentionCheckHander receiveIntentionCheckHander;

    @Autowired
    EmployeeDisabledHander employeeDisabledHander;

    @Autowired
    PushEmployeeInfoHander pushEmployeeInfoHander;

    /**
     * 请求鉴权token
     * @param request
     * @param reqVo
     * @return
     */
    @V10Log
    @PostMapping(value = "/common/getToken", produces = "application/json")
    public String getToken(HttpServletRequest request, @RequestBody GetTokenReqVo reqVo){
        try {
            return JSONUtil.toJSONStringWithDateFormat(getTokenForHander.hander(reqVo));
        } catch (Exception e) {
            logger.error("getToken error: {}", e);
            return null;
        }
    }

    /**
     * v10 - channle 报备数据同步
     * @param request
     * @param reqVo
     * @return
     */
    @V10Log
    //@CheckToken
    @PostMapping(value = "/premises/receiveIntention", produces = "application/json")
    public String receiveIntention(HttpServletRequest request, @RequestBody ReceiveIntentionReqVo reqVo){
        try {
            return JSONUtil.toJSONStringWithDateFormat(receiveIntentionHander.hander(reqVo));
        } catch (Exception e) {
            logger.error("receiveIntention error: {}", e);
            return null;
        }
    }
    @V10Log
    @CheckToken
    @PostMapping(value = "/premises/receiveVisit", produces = "application/json")
    public String receiveVisit(HttpServletRequest request, @RequestBody ReceiveVisitReqVo reqVo){
        try {
            return JSONUtil.toJSONStringWithDateFormat(receiveVisitHander.hander(reqVo));
        } catch (Exception e) {
            logger.error("receiveVisit error: {}", e);
            return null;
        }
    }
    
    @V10Log
    @CheckToken
    @PostMapping(value = "/premises/syncIntentionFollow", produces = "application/json")
    public String syncIntentionFollow(HttpServletRequest request, @RequestBody SyncFollowReqVo reqVo){
        try {
            return JSONUtil.toJSONStringWithDateFormat(receiveIntentionFollowAffixHander.hander(reqVo));
        } catch (Exception e) {
            logger.error("syncIntentionFollow error: {}", e);
            return null;
        }
    }
    
    @V10Log
    @CheckToken
    @PostMapping(value = "/premises/syncAffix", produces = "application/json")
    public String syncAffix(HttpServletRequest request, @RequestBody SyncAffixReqVo reqVo){
        try {
            return JSONUtil.toJSONStringWithDateFormat(receiveAffixHander.hander(reqVo));
        } catch (Exception e) {
            logger.error("syncAffix error: {}", e);
            return null;
        }
    }
    
    @SuppressWarnings("unchecked")
	@V10Log
    @CheckToken
    @PostMapping(value="/premises/listEstateAdviserByPremisesId")
    public BaseResponseDTO<List<EstateAdviserDto>> listEstateAdviserByPremisesId(@RequestBody @Valid Listv10EstateAdviserByPremisesIdReqVo request,BindingResult bindingResult) {
    	try {
    		return (BaseResponseDTO<List<EstateAdviserDto>>)listEstateAdviserByPremisesIdHandler.hander(request,bindingResult);
        } catch (Exception e) {
            logger.error("listEstateAdviserByPremisesId error: {}", e);
            return null;
        }
    	
    }

    /**
     *  查询V10报备在渠道端的报备、交易状态
     * @param premisesIntentionReqVo
     * @return
     */
    @V10Log
    @CheckToken
    @PostMapping(value="/premises/queryIntentionStatus")
    public BaseResponseDTO<PremisesIntentionResDto> queryIntentionStatus(@RequestBody @Valid PremisesIntentionReqVo premisesIntentionReqVo){
        try {
            return (BaseResponseDTO<PremisesIntentionResDto>)queryIntentionStatusHander.hander(premisesIntentionReqVo);
        } catch (Exception e) {
            logger.error("queryIntentionStatus error: {}", e);
            return null;
        }
    }

    @V10Log
    @CheckToken
    @PostMapping(value="/premises/intention/check")
    public String premisesIntentionCheck(HttpServletRequest request, @RequestBody ReceiveIntentionReqVo reqVo){
        try {
            return JSONUtil.toJSONStringWithDateFormat(receiveIntentionCheckHander.hander(reqVo));
        } catch (Exception e) {
            logger.error("queryIntentionStatus error: {}", e);
            return null;
        }
    }

    @V10Log
    @CheckToken
    @PostMapping(value="/employee/disabled")
    public String employeeDisabled(HttpServletRequest request, @RequestBody EmployeeDisabledReqVo reqVo){
        try {
            return JSONUtil.toJSONStringWithDateFormat(employeeDisabledHander.hander(reqVo));
        } catch (Exception e) {
            logger.error("employeeDisabled error: {}", e);
            return null;
        }
    }

    @V10Log
    @PostMapping(value="/push/employee")
    public String pushEmployee(){
        try {
            BaseRequestDTO<String> requestDTO = new BaseRequestDTO<>();
            // 设置一个默认值，无实际意义
            requestDTO.setParams("1");
            return JSONUtil.toJSONStringWithDateFormat(pushEmployeeInfoHander.hander(requestDTO));
        } catch (Exception e) {
            logger.error("pushEmployee error: {}", e);
            return null;
        }
    }

}
